CREATE FUNCTION DBO.TRB_IPTU_QUITADO( @IMV_COD INT, @ANO INT)
RETURNS BIT
AS
BEGIN
	DECLARE @QUNICA INT, @QPARCELA INT, @R BIT
	
	SELECT @QUNICA = COUNT(*) FROM TRB_IPTU I JOIN TRB_LANCAMENTO L ON L.TLA_COD = I.TLA_COD
		JOIN TRB_BAIXA B ON B.LAN_COD = L.LAN_COD
		WHERE I.EXE_ANO = @ANO AND LAN_PARCELA = 0 AND I.IMV_COD = @IMV_COD
	IF @QUNICA = 0
		SELECT @QPARCELA = COUNT(*) FROM TRB_IPTU I JOIN TRB_LANCAMENTO L ON L.TLA_COD = I.TLA_COD
			JOIN TRB_BAIXA B ON B.LAN_COD = L.LAN_COD
			WHERE I.EXE_ANO = @ANO AND LAN_PARCELA > 0 AND I.IMV_COD = @IMV_COD
	
	IF @QUNICA > 0 OR @QPARCELA > 9
		SET @R = 1
	ELSE
		SET @R = 0
	RETURN @R
END
GO